<?php
class Template
{
	var $var_names = array();
	var $files = array();
	var $start = '{';
	var $end = '}';
	
	function open_template($file_id, $filename)
	{
		$this -> files[$file_id] = fread($fp = fopen($filename, 'r'), filesize($filename));
		fclose($fp);
	}
	
	function set_identifiers($start, $end)
	{
		$this -> start = $start;
		$this -> end = $end;
	}
	
	function traverse_array($file_id, $array)
	{
		while(list(, $value) = each($array))
		{
			if(is_array($value))
			{
				$this -> traverse_array($file_id, $value);
			}
			else
			{
				$this -> var_names[$file_id][] = $value;
			}
		}
	}
	
	function register($file_id, $var_name)
	{
		if(is_array($var_name))
		{
			$this -> traverse_array($file_id, $var_name);
		}
		elseif($var_name != '')
		{
			if(is_long(strpos($var_name, ',')) == TRUE)
			{
				$var_name = explode(',', $var_name);
				
				for(reset($var_name); $current = current($var_name); next($var_name))
				{
					$this -> var_names[$file_id][] = trim($current);
				}
			}
			else
			{
				$this -> var_names[$file_id][] = $var_name;
			}
		}
	}
	
	function include_file($file_id, $filename)
	{
		if(is_file($filename))
		{
			$include = fread($fp = fopen($filename, 'r'), filesize($filename));
			fclose($fp);
		}
		else
		{
			$include = 'File does not exist';
		}
		
		$tag = substr($this -> files[$file_id], strpos(strtolower($this -> files[$file_id]), 
		'{include file="' . $filename . '"}'), strlen('{include file="' . $filename . '"}'));
		$this -> files[$file_id] = str_replace($tag, $include, $this -> files[$file_id]);
	}
	
	function parse($file_id)
	{
		$file_ids = explode(',', $file_id);
		
		for(reset($file_ids); $file_id = trim(current($file_ids)); next($file_ids))
		{
			while(is_long($pos = strpos(strtolower($this -> files[$file_id]), '{include file="')))
			{
				$pos += 19;
				$endpos = strpos($this -> files[$file_id], '"}', $pos);
				$filename = substr($this -> files[$file_id], $pos, $endpos - $pos);
				$this -> include_file($file_id, $filename);
			}
			
			if(isset($this -> var_names[$file_id]) AND count($this -> var_names[$file_id]) > 0)
			{
				for($i = 0; $i < count($this -> var_names[$file_id]); $i++)
				{
					$temp_var = $this -> var_names[$file_id][$i];
					global $$temp_var;
					$this -> files[$file_id] = str_replace($this -> start . $temp_var . $this -> end, 
					$$temp_var, $this -> files[$file_id]);
				}
			}
		}
	}
	
	function parse_loop($file_id, $array_name)
	{
		global $$array_name;
		$loop_code = '';
		
		$file = explode(chr(10), $this -> files[$file_id]);

		$start_pos = strpos(strtolower($this -> files[$file_id]), 
		'{loop name="'.$array_name.'"}') + strlen('{loop name="'.$array_name.'"}');
	
		$end_pos = strpos(strtolower($this -> files[$file_id]), '{/loop name="'.$array_name.'"}');
	
		$loop_code = substr($this -> files[$file_id], $start_pos, $end_pos - $start_pos);
		
		$start_tag = substr($this -> files[$file_id], strpos(strtolower($this -> files[$file_id]), 
		'{loop name="'.$array_name.'"}'), strlen('{loop name="'.$array_name.'"}'));

		$end_tag = substr($this -> files[$file_id], strpos(strtolower($this -> files[$file_id]), 
		'{/loop name="'.$array_name.'"}'), strlen('{/loop name="'.$array_name.'"}'));
		
		if($loop_code != '')
		{
			$new_code = '';
			for($i = 0; $i < count($$array_name); $i++)
			{
				$temp_code = $loop_code;
				
				while(list($key,) = each(${$array_name}[$i]))
				{
					if(${$array_name}[$i]['type'] == 'php')
					{
						if($key == 'content')
						{
							$foobar = stripslashes(${$array_name}[$i][$key]);
							ob_start();
							eval($foobar);
							$chronoz = ob_get_contents();
							ob_end_clean();
							$temp_code = str_replace($this -> start . $key . $this -> end, $chronoz, $temp_code);
						}
						else
						{
							$temp_code = str_replace($this -> start . $key . $this -> end, ${$array_name}[$i][$key], $temp_code);
						}
					}
					else
					{
						$temp_code = str_replace($this -> start . $key . $this -> end, stripslashes(${$array_name}[$i][$key]), $temp_code);
					}
				}
				
				$new_code .= $temp_code;
			}
			$this -> files[$file_id] = str_replace($start_tag . $loop_code . $end_tag, $new_code, $this -> files[$file_id]);
		}
	}
	
	function print_file($file_id)
	{
		if(is_long(strpos($file_id, ',')) == TRUE)
		{
			$file_id = explode(',', $file_id);
			for(reset($file_id); $current = current($file_id); next($file_id))
			{
				echo $this -> files[trim($current)];
			}
		}
		else
		{
			echo $this -> files[$file_id];
		}
	}
	
	function return_file($file_id)
	{
		$ret = '';
		
		if(is_long(strpos($file_id, ',')) == TRUE)
		{
			$file_id = explode(',', $file_id);
			for(reset($file_id); $current = current($file_id); next($file_id))
			{
				$ret .= $this -> files[trim($current)];
			}
		}
		else
		{
			$ret .= $this -> files[$file_id];
		}
		
		return $ret;
	}
	
	function template_output($file_id, $replacements = '')
	{
		$this -> register($file_id, $replacements);
		$this -> parse($file_id);
		$this -> print_file($file_id);
	}
	
	function tpl_assign($value)
	{
		global $assigned;
		
		$assigned[] = $value;
		
		return $assigned;
	}
}
?>